// pkgTask/pages/takephoto-task-list/index.js
const lbbTaskService = require('../service/task-service.js');
const lbbService = require('../../../pages/service/service.js');
import fileUtil from '../../../utils/fileUtil.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    lbmId: 0,
    hosBetTypeList:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      this.data.lbmId = options.lbmId
      this.data.hosRecordId = options.hosRecordId
      this.setData({
        lbmName: options.lbmName,
        taskTime: options.taskTime,
        taskStat: options.taskStat
      })

      this.getPutEquTypeList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
     this.getHosPhotoInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 跳转到设备拍摄页面
   */
  navToTakePhotoUpload(e) {
    let that = this
    let data = e.currentTarget.dataset
    console.log("toUploadPhoto-->", data)
    wx.navigateTo({
      url: '/pkgTask/pages/task-takephoto-upload/index?lbmId=' + that.data.lbmId + '&hosRecordId=' + that.data.hosRecordId 
      + '&equId=' + data.equid + '&lockType=' + data.locktype + '&equRecordId=' + data.id + '&photoFlg=' + data.photoflg,
    })
  },

  getPutEquTypeList(){
    let that = this
    lbbService.getHosPutEquTypeList({
      lbmId:that.data.lbmId
    },
      function(res) {
         that.setData({
          hosBetTypeList: res.data
         })
      })
  },

  changeEquType(e) {
    let that = this
    let equTypeIndex = e.detail
    let betTypeInfo = this.data.hosBetTypeList[equTypeIndex]
    console.log("changeEquType-->", equTypeIndex, betTypeInfo);
    this.data.equCategoryId = betTypeInfo.categoryId
    let selEquList = that.data.allEquMap[that.data.equCategoryId]
    selEquList = that.handleEquList(selEquList)
    let passCount = that.data.checkPassCount[that.data.equCategoryId]
    let rejectCount = that.data.checkRejectCount[that.data.equCategoryId]
    this.setData({
      equList: selEquList ? selEquList : [],
      passCount: passCount ? passCount : 0,
      rejectCount: rejectCount ? rejectCount : 0
    })
  },

  getHosPhotoInfo() {
    let that = this
      lbbTaskService.getPhotoEquList({
        hospitalRecordId: that.data.hosRecordId,
        lbmId:that.data.lbmId
      }, function(res) {
        let equList = res.data
        that.data.allEquMap = equList
        that.data.checkPassCount = res.checkPassCount
        that.data.checkRejectCount = res.checkRejectCount
        if(!that.data.equCategoryId) {
          that.data.equCategoryId = res.categoryId
        }
        that.setData({
          equList: that.handleEquList(equList[that.data.equCategoryId]),
          passCount: that.data.checkPassCount[that.data.equCategoryId],
          rejectCount: that.data.checkRejectCount[that.data.equCategoryId]
        }) 
      })
  },

  handleEquList(equList) {
    let that = this
    if (equList && equList.length > 0) {
      equList.forEach((equ) => {
        let localID = `${that.data.lbmId}-${that.data.hosRecordId}-${equ.id}`
        equ._notUploadCount = fileUtil.getNotUploadCount(localID)
      })
    }
    return equList
  },
  

})